//
//  GPAuthorizationManager.m
//  PFAmapDemo
//
//  Created by 原鹏飞 on 2018/10/31.
//  Copyright © 2018 ypf. All rights reserved.
//

#import "GPAuthorizationManager.h"

@implementation GPAuthorizationManager

// 创建单例
+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static GPAuthorizationManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[GPAuthorizationManager alloc] init];
    });
    return instance;
}


- (void)requestAuthorization {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}


- (CTCellularDataRestrictedState)gp_checkNetworkAuthorization {
    CTCellularData *cellularData = [[CTCellularData alloc] init];
    NSLog(@"%zd", cellularData.restrictedState);
    return cellularData.restrictedState;
//    cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){
//        //获取联网状态
//        switch (state) {
//            case kCTCellularDataRestricted:
//                NSLog(@"Restricrted");
//                [self requestAuthorization];
//                break;
//            case kCTCellularDataNotRestricted:
//                NSLog(@"Not Restricted");
//                break;
//            case kCTCellularDataRestrictedStateUnknown:
//                NSLog(@"Unknown");
////                return @"Unknown"
//                break;
//            default:
//                break;
//        };
//    };
}


- (void)gp_checkNetworkState {
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    //    [[Reachability reachabilityWithHostName:@"www.baid.com"] startNotifier];
    
    
    // 判断网络状态
    if ([[Reachability reachabilityWithHostName:@"www.Apple.com"] isReachable]) {
        NSLog(@"有网");
        //        return YES;
    } else {
        NSLog(@"没网");
    }
    
    
    //    return NO;
}


- (void)gp_checkLocationAuthorization {
    BOOL isLocation = [CLLocationManager locationServicesEnabled];
    if (!isLocation) {
        NSLog(@"not turn on the location");
    }
    CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
    switch (CLstatus) {
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"Always Authorized");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"AuthorizedWhenInUse");
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"Denied");
            break;
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"not Determined");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"Restricted");
            break;
        default:
            break;
    }
    
}

@end
